//axios公共组件函数
//引入axios
import store from '@/store'
import axios from 'axios'
import { Message } from 'element-ui'
// 引入store仓库

//封装公共地址 超时时间
const instance = axios.create({
    baseURL: 'http://sell.h5.itsource.cn:8087',
    timeout: 1000

})
// 封装请求拦截器
instance.interceptors.request.use(
    // config 请求信息对象
    // 获取token
    config => {
        const token = store.getters.token
        // token挂载在config。headers的某个字段
        config.headers.authorization = token
        return config
    },
    err => {
        return Promise.reject(err)
    }
)
// 响应拦截器
instance.interceptors.response.use(
    response => {
        //处理成功状态ma
        console.log(response);
        let { code, msg } = response.data

        if (code === 0) {
            Message.success(msg)
        }
        if (code === 1) {
            Message.error(msg)
        }
        if (code === 5001) {
            Message.error(msg)
        }




        return response
    },
    err => {

        let { Message, response } = err
        if (response) {
            switch (response.status) {
                case 401:
                    Message.error(message)
                    break;
                case 403:
                    Message.error(message)
                    break;
                case 404:
                    Message.error(message)
                    break;
                case 405:
                    Message.error(message)
                    break;

            }
        }
        return Promise.reject(err)
    }

)
export default instance